<?php 
## include required files
/*******************************/
require_once '../model/cms.php';
require_once '../model/moduleuser.php';
## Paginaton class
require_once ("../includes/classes/pagination_class.php");
/*******************************/

## Create Objects
/*******************************/
$cmsObj = new Model_CMS();
$moduleObj=new Model_ModuleUser();
/*******************************/

## Active, Inactive selected records
/*******************************/
if(isset($_POST['go']) != '' && isset($_POST['action']) != '') {
		$ids = implode(",", $_POST['checkall']);
			## check which action is selected
		$hidden_page = $_POST['hidden_pageno'];	
		if($_POST['action'] == 'active') {
			## Active selected records
			$cmsObj->updateMultipleCmsStatus($ids, '1');
			$_SESSION['msg']="<div class='success_msg'><span>CMS pages activated successfully</span></div>";
		} else { 
			## Inactive selected records
			$cmsObj->updateMultipleCmsStatus($ids, '0');
			$_SESSION['msg']="<div class='success_msg'><span>CMS pages deactivated successfully</span></div>";
		} 
		header('location:'.SITE_URL.'/admin/home.php?q=cms&page='.$hidden_page);
		exit;
		## Delete code will be here if required
}

## Active/Inactive thourgh URL
if(isset($_GET['action']) == 'status' && isset($_GET['status']) != '') {
	$id = base64_decode($_GET['id']);
	## Update cms page status 
	$array = array();
	$array['status'] = ($_GET['status']=='1'?0:1);
	$cmsObj->editUserCmsById($array, $id);
	$_SESSION['msg']="<div class='success_msg'><span>CMS page status updated successfully</span></div>";
	header('location:'.SITE_URL.'/admin/home.php?q=cms&page='.$_GET['page']);
	exit;
}
/*******************************/
if($_GET['q']=='cms')
{
	$moduelArray=$moduleObj->getModuleHelpText(1);
	$smarty->assign('moduelArray', $moduelArray);
	//echo '<pre>';print_r($moduelArray);exit;
}
##Fetch all cms pages from database
## Get search parameters in variables - 
if($_POST['search'] != '' && $_POST['search'] != 'Search') {
	$searchindex = addslashes($_POST['search']);
} else if($_GET['search'] != '') {
	$searchindex = addslashes($_GET['search']);
} else {
	$searchindex = '';
}
$searchindex=trim($searchindex);
$smarty->assign('search', stripslashes (stripslashes ($searchindex)));

if($_GET['col1'] != '') {
	$col1 = base64_decode($_GET['col1']);
} else {
	$col1 = '0';
}
##  --------- Pagination part first start --------------##
	if( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] != "" ) {
		$pageNum = $_GET[ 'page' ];
	} else {
		$pageNum = 1;
	} 
	$rowsPerPage = ROW_PER_PAGE;
	if($_GET['col1'] != '') {
		$pageName = "home.php?q=cms&col1=".$_GET['col1'].""; 
	} else {
		$pageName = "home.php?q=cms"; 	
	}
	## Count all the records
	$cmsArray = $cmsObj->getAllSubMenu($searchindex,$col1);
	$total_rows = count($cmsArray);
	$offset	= ($pageNum - 1) * $rowsPerPage;
	$smarty->assign('page', $pageNum);
##  --------- Pagination part first end --------------##


##  --------- Pagination part second start --------------##
	$newid = $pageNum * $rowsPerPage - $rowsPerPage + 1;
	$smarty->assign('newid', $newid);
	$cmsArray = $cmsObj->getAllSubMenu($searchindex,$col1,$rowsPerPage,$offset);
	//echo "<pre>"; print_r($cmsArray);exit;
	$smarty->assign('cmsArray', $cmsArray);
	if($total_rows > ROW_PER_PAGE) {
		$pg = new pagination();					// CREATING OBJECT OF THE PAGINATION CLASS
		$pgnation = $pg->pagination_admin( $total_rows , $rowsPerPage , $pageNum , $pageName , $other_id, $class);	
		$smarty-> assign("pagination",$pgnation);
	}
##  --------- Pagination part second end --------------##	

## Assign session message to smarty variable and unset session variable
if($_SESSION['msg'] != '') {
	$smarty->assign('msg', $_SESSION['msg']);	
	unset($_SESSION['msg']);
}

## Set active class variable for left menu (main menu and sub menu)
$smarty->assign('activeclass', 'cms');
$smarty->assign('mainmenu', '1');

## Unset all the objects created which are on this page
unset($cmsObj);
unset($moduleObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'cms/cms.tpl');
unset($smarty);
?>